%% Model Properties
% by IMF Institute for Capacity Development - Course on Monetary and Exchange Rate Policies

%% Clear the workspace
close all;
clear all;
disp('Working ...');

%% Read the model
readmodel;

%% Sets the time frame for the simulation 
startsim = qq(2013,1);
endsim = qq(2015,4); % three-year simulation horizon

%% List of shocks
% Temporary shocks to: inflation, output, exchange rate, interest rate
% Creates a list (vector) of shocks and list of their names. Used shocks' names must
% be the names found in the model code (in file 'model.mod') except of the prefix
% 'shock_'
listshocks = {'shk_dot_cpi','shk_lgdp_gap','shk_ls','shk_rn'};
listtitles = {'Inflationary Shock', 'Aggregate Demand Shock', 'Exchange Rate Shock', 'Interest Rate Shock'};

% For each shock a zero database is created (command 'zerodb') and named as 
% database 'd.{shock_name}'
for i = 1:length(listshocks);
    d.(listshocks{i}) = zerodb(m,startsim-4:endsim);
end

%% Calibration of shocks
% Fills the respective databases with the shocks' values for the starting
% point of the simulation (startsim). For simplicity, all shocks are set to
% 1 percent
d.shk_dot_cpi.shk_dot_cpi(startsim) = 1;
d.shk_lgdp_gap.shk_lgdp_gap(startsim) = 1;
d.shk_ls.shk_ls(startsim) = -1;
d.shk_rn.shk_rn(startsim) = 1;
% d.shk_prem.shk_prem(startsim) = 1;

%% Simulates the shocks using the command 'simulate'. For this model
% 'm' and respective database 'd.{shock_name} are used. Results are written 
% in object 's.{shock_name}'. Command 'dboverlay' merges the historical 
% database d.{shock_name} with simulation database s.{shock_name}. 
for i=1:length(listshocks);    
    s.(listshocks{i}) = simulate(m,d.(listshocks{i}),startsim:endsim,'deviation',true);
    s.(listshocks{i}) = dbextend(d.(listshocks{i}),s.(listshocks{i}));

% % Cumulative output gap
% s.(listshocks{i}).cum_gap = tseries(startsim-1,0);
%     for ii = 1:length(startsim:endsim)
%     s.(listshocks{i}).cum_gap(startsim:endsim) = s.(listshocks{i}).cum_gap{-1} + s.(listshocks{i}).lgdp_gap;
%     end
% s.(listshocks{i}).cum_gap.comment = (listshocks{i});
% disp('Cumulative output gap for');
% s.(listshocks{i}).cum_gap 

end

%% Report
x = report.new('Shocks','visible',true);

% Figures
sty = struct();
sty.line.linewidth = 1.5;
sty.line.linestyle = {'-';'--'};
sty.line.color = {'k';'k'};
sty.axes.box = 'on';
sty.legend.location = 'southOutside';
sty.legend.orientation = 'horizontal';

for i = 1:length(listshocks);
x.figure(listtitles{i},'subplot',[2,3],'style',sty,'range',startsim:endsim,...
  'dateformat','YYYY:P','dateTick',startsim:4:endsim);

x.graph('Inflation','legend',true);
x.series('',[s.(listshocks{i}).dot_cpi s.(listshocks{i}).dot4_cpi],'legend',{'q-o-q','y-o-y'});

x.graph('Nominal Interest Rate','legend',false);
x.series('',[s.(listshocks{i}).rn]);

x.graph('Nominal Exchange Rate (q-o-q)','legend',false);
x.series('',[s.(listshocks{i}).dot_s]);

x.graph('Output Gap','legend',true);
x.series('',[s.(listshocks{i}).lgdp_gap s.(listshocks{i}).cum_gap],...
'legend',{'level','cumul'});

x.graph('Real Interest Rate Gap','legend',false);
x.series('',[s.(listshocks{i}).rr_gap]);

x.graph('Real Exchange Rate Gap','legend',false);
x.series('',[s.(listshocks{i}).lz_gap]);
  
x.pagebreak();

end

% %% Disinflationary Shock, 1%
% % Creates a steady-state database (a database filled with steady-state
% % values) and sets the target to be lower for 1% over the simulation
% % horizon
% d.disinfl = sstatedb(m,startsim-4:endsim+40);
% d.disinfl.target(startsim+1:endsim+40) =  d.disinfl.target - 1;
% 
% % Sets a simulation plan where the target is taken from the database (lower
% % for 1% in comparison to the initial value)
% simplan = plan(m,startsim+1:endsim+40);
% simplan = exogenize(simplan,'target',startsim+1:endsim+40);
% simplan = endogenize(simplan,'shk_target',startsim+1:endsim+40);
% 
% % Simulates the disinflation
% s.disinfl = simulate(m,d.disinfl,startsim:endsim+40,'plan',simplan);
% 
% % Graph
% % Makes graph for the disinflation
% x.figure('1% Disinflation','subplot',[2,3],'style',sty,'range',startsim:endsim,'dateformat','YYYY:P');
% 
% x.graph('Inflation','legend',true);
% x.series({'q-o-q','y-o-y'},[s.disinfl.dot_cpi s.disinfl.dot4_cpi]);
% 
% x.graph('Nominal Interest Rate','legend',false);
% x.series('',[s.disinfl.rn]);
% 
% x.graph('Nominal Exchange Rate (q-o-q)','legend',false);
% x.series('',[s.disinfl.dot_s]);
% 
% x.graph('Output Gap','legend',true);
% x.series({'Output Gap','Cumulative Gap'},[s.disinfl.lgdp_gap s.disinfl.cum_gap]);
% 
% x.graph('Real Interest Rate Gap','legend',false);
% x.series('',[s.disinfl.rr_gap]);
% 
% x.graph('Real Exchange Rate Gap','legend',true);
% x.series('',[s.disinfl.lz_gap]);

%% Make the report
x.publish('Report_Shocks.pdf','display',false);
disp('Done!');
